<cfcomponent output="false">

	<!--- --->

	<cffunction name="saveGoal" access="public" output="true" returntype="void">
		
		<cfset var local = {} />
		
		<cftransaction>
			<cfset local.goalID = $.run.it('goalService.saveGoal', arguments) />
			
			<cfif arguments.goalID eq "">
				<cfset $.run.it('goalService.saveMilestone', {
					date=arguments.start,
					description="Goal Start (edit me)",
					goalID=local.goalID,
					locked=-1
				}) />
				<cfset $.run.it('goalService.saveMilestone', {
					date=arguments.stop,
					description="Goal End (edit me)",
					goalID=local.goalID,
					locked=1,
					target_count=arguments.target_count
				}) />
				<cfset $.run.it('goalService.generateMilestones',{
					goalID=local.goalID,
					startdate=arguments.start,
					enddate=arguments.stop,
					milestone_frequency=arguments.milestone_frequency,
					target=arguments.target
				}) />
			<cfelse>
				<cfset local.start_milestone = $.run.it('goalService.getMilestones',{goalid=arguments.goalID,locked="-1"}) />
				<cfset local.stop_milestone = $.run.it('goalService.getMilestones',{goalid=arguments.goalID,locked="1"}) />
				<cfif local.start_milestone.id neq "">
					<cfset arguments.start = $.date.localToUTC(arguments.start) />
					<cfset $.database.save(table="goal_milestone",id=local.start_milestone.id,date=arguments.start) />
				</cfif>
				<cfif local.stop_milestone.id neq "">
					<cfset arguments.stop = $.date.localToUTC(arguments.stop) />
					<cfset $.database.save(table="goal_milestone",id=local.stop_milestone.id,date=arguments.stop,target_count=arguments.target) />
				</cfif>
			</cfif>
		</cftransaction>
		
		<cflocation url="#cgi.HTTP_REFERER#" addToken="no" />

	</cffunction>

	<!--- --->

	<cffunction name="deleteGoal" access="public" output="true" returntype="void">
		<cfargument name="goalID" required="true" />
		
		<cftransaction>
			<cfset $.run.it('goalService.deleteGoal', arguments) />
			<cfset $.run.it('goalService.bulkDeleteMilestones', arguments) />
			<cfset $.run.it('goalService.bulkDeleteActions', arguments) />
			<cfset $.run.it('workweekSErvice.bulkDeleteWorkweekEvents',arguments)>
		</cftransaction>
		
		<cflocation url="#cgi.HTTP_REFERER#" addToken="no" />

	</cffunction>

	<!--- --->

	<cffunction name="deleteMilestone" access="public" output="true" returntype="void">
		<cfargument name="milestoneID" required="true" />
		
		<cfset $.run.it('goalService.deleteMilestone', arguments) />
		
		<cflocation url="#cgi.HTTP_REFERER#" addToken="no" />

	</cffunction>

	<!--- --->

	<cffunction name="deleteAction" access="public" output="true" returntype="void">
		<cfargument name="actionID" required="true" />
		
		<cftransaction>
			<cfset $.run.it('goalService.deleteAction', arguments) />
			<!--- delete all workweek actions with this actionID --->
			<cfset $.run.it('workweekService.bulkDeleteWorkweekEvents',{actionID=arguments.actionID}) />
		</cftransaction>
		
		<cflocation url="#cgi.HTTP_REFERER#" addToken="no" />

	</cffunction>

	<!--- --->
	
	<cffunction name="saveMilestone" access="public" output="true" returntype="void">
		<cfargument name="milestoneID" required="false" default="" />
		<cfargument name="goalID" required="true" />
		<cfargument name="date" required="true" />
		<cfargument name="description" required="true" />
		<cfargument name="complete" required="false" default="false" />
		<cfargument name="time_frame" required="false" default="false" />

		<cfset var local = {} />
		
		<cfset local.milestoneID = $.run.it('goalService.saveMilestone', arguments) />
		
		<cflocation url="#$.site.getVar('rootURL')#/craftivity/goals.cfm?showMilestoneGoalID=#arguments.goalID#&time_frame=#arguments.time_frame#" addToken="no" />

	</cffunction>

	<!--- --->

	<cffunction name="saveAction" access="public" output="true" returntype="void">
		<cfargument name="actionID" required="true" />
		<cfargument name="goalID" required="true" />
		<cfargument name="name" required="true" />
		<cfargument name="time_frame" required="false" default="" />

		<cfset var local = {} />
		
		<cftransaction>
			<cfset $.run.it('goalService.saveAction', arguments) />
	
			<cfif arguments.old_name neq arguments.name>
				<cfset local.workweekEvents = $.run.it('workweekService.getWorkweekEvents',{goalActionID=arguments.actionID}) />
	
				<cfloop query="local.workweekEvents">
					<cfset $.database.save(
						table="workweek_event",
						id=local.workweekEvents.ID,
						name=arguments.name
					) />
				</cfloop>
			</cfif>
		</cftransaction>

		<cflocation url="#$.site.getVar('rootURL')#/craftivity/goals.cfm?showActionGoalID=#arguments.goalID#&time_frame=#arguments.time_frame#" addToken="no" />

	</cffunction>

	<!--- --->

	<cffunction name="saveGoalActionWorkweekEvents" access="public" output="true" returntype="void">
		<cfargument name="actionID" required="false" default="" />
		<cfargument name="goalID" required="true" />
		<cfargument name="time_frame" required="false" default="" />

		<cfset var local = {} />

		<cfset local.actionID = $.run.it('goalService.saveAction', arguments) />

		<cfset local.duration = 0 />
		<cfif isNumeric(arguments.duration_hour)>
			<cfset local.duration = arguments.duration_hour * 60 />
		</cfif>
		<cfset local.duration = local.duration + arguments.duration_minute />

		<cftransaction>
			<cfloop list="#arguments.days#" index="local.i" item="local.day">
				<cfset $.run.it('workweekService.saveWorkweekEvent', {
					name = arguments.name,
					duration = local.duration,
					weekday = local.day,
					goalActionID = local.actionID,
					category=arguments.category
				}) />
			</cfloop>
		</cftransaction>

		<cflocation url="#$.site.getVar('rootURL')#/craftivity/goals.cfm?showActionGoalID=#arguments.goalID#&time_frame=#arguments.time_frame#" addToken="no" />

	</cffunction>

	<!--- --->

</cfcomponent>